﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using permit.App_Code;
using permit.App_Code.DataAccess;
using permit.App_Code.Entity;
using System.Data;

namespace permit.PollingTable
{
    public partial class Polling : System.Web.UI.Page
    {
        DataSet ds;
        PermitAccess perAcc = new PermitAccess();
        DepartmentAccess depAcc = new DepartmentAccess();
        PrinterAccess priAcc = new PrinterAccess();
        DataSet dsTable;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserTable user = (UserTable)Session["UserTable"];
                if (user == null)
                {
                    Response.Write("<script>alert('请先登录！')</script>");
                    Response.Redirect("~/default.aspx");
                }

                if (user.DEP_ID != 17)
                {
                    Response.Redirect("~/main/Main.aspx?Sturt=PrintTable");
                }
                DataSet ds = depAcc.GetDepartments();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ListItem li = new ListItem(dr["dep_Name"].ToString(), dr["dep_AreaCode"].ToString());
                    ddlDepArea.Items.Add(li);
                }
                txtDate.Text = DateTime.Now.Year.ToString() + '-' + DateTime.Now.Month.ToString();

                dsTable = priAcc.GetPrintLable();
              
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (txtDate.Text.Length < 6)
            {
                lblInfo.Text = "请输入年份-月份！";
                return;
            }

            string[] strDate = txtDate.Text.Split('-');

            if (strDate.Length > 1)
            {
                if (MeetingManagerLib.DataTypeCheck.IsDigit(strDate[0]) && MeetingManagerLib.DataTypeCheck.IsDigit(strDate[1]))
                {
                    ds = perAcc.GetPollPermits(ddlDepArea.SelectedValue, Convert.ToInt32(strDate[0]), Convert.ToInt32(strDate[1]));
                    gvPermit.DataSource = ds;
                    gvPermit.DataBind();
                }
                else
                {
                    lblInfo.Text = "请输入年份-月份！";
                    return;
                }
            }
        }

        protected void gvPermit_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPermit.PageIndex = e.NewPageIndex;
            string[] strDate = txtDate.Text.Split('-');
            ds = perAcc.GetPollPermits(ddlDepArea.SelectedValue, Convert.ToInt32(strDate[0]), Convert.ToInt32(strDate[1]));
            gvPermit.DataSource = ds;
            gvPermit.DataBind();
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            string[] strDate = txtDate.Text.Split('-');
            ds = perAcc.GetPollPermits(ddlDepArea.SelectedValue, Convert.ToInt32(strDate[0]), Convert.ToInt32(strDate[1]));
            if (ds.Tables[0].Rows.Count > 0)
            {
                Session["PrintTable"] = ds;
                Page.RegisterStartupScript("printTable", string.Format("<script>window.open('printTable.aspx?dep={0}&data={1}')</script>", ddlDepArea.SelectedItem.Text, txtDate.Text));
            }
        }

        protected void gvPermit_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                int indexID = this.gvPermit.PageIndex * this.gvPermit.PageSize + e.Row.RowIndex + 1;
                e.Row.Cells[0].Text = indexID.ToString();
            }
        }

      


    }
}
